
package com.think.teaching.controller;


import com.think.common.model.Result;
import com.think.common.model.ResultCode;
import com.think.teaching.model.TrainingTest;
import com.think.teaching.service.impl.TrainingTestServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
@RestController
@RequestMapping("/v1")
@ResponseBody
@Api(tags = "实训测试管理接口")
public class TrainingTestController {
	
	private final TrainingTestServiceImpl trainingTestService;

	public TrainingTestController(TrainingTestServiceImpl trainingTestService) {
		this.trainingTestService = trainingTestService;
	}

	@ApiOperation("添加新的实训测试")
	@PostMapping("admin/trainingTestTest")
	public Result AddNewTrainingTestTest(@RequestBody()TrainingTest data) {
		Result result = data.CheckObjectComplete();
		if (result.getCode() == ResultCode.SUCCESS.code()) {
			TrainingTest trainingTest = trainingTestService.AddTrainingTest(data);
			if (trainingTest != null) {
				return Result.success(trainingTest);
			} else {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		} else
			return result;
	}
	
	@ApiOperation("更新实训测试")
	@RequestMapping(value = "admin/trainingTestTest",method = RequestMethod.PUT)
	public Result UpdateTrainingTestTest(@RequestBody()TrainingTest data) {
		Result result = data.CheckObjectComplete();
		if (result.getCode() == ResultCode.SUCCESS.code()) {
			TrainingTest trainingTest = trainingTestService.UpdateTrainingTest(data);
			if (trainingTest != null) {
				return Result.success(trainingTest);
			} else {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		} else
			return result;
	}
	
	@ApiOperation("删除实训测试")
	@RequestMapping(value = "admin/trainingTestTest/{id}",method = RequestMethod.DELETE)
	public Result DeleteTrainingTestTest(@PathVariable("id")Integer id) {
		TrainingTest trainingTest = trainingTestService.getById(id);
		if (trainingTest == null) {
			return Result.failure(ResultCode.TRAINING_TEST_NOT_EXIST);
		} else {
			if (trainingTestService.DeleteObjectById(id)) {
				return Result.success();
			}
			return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
		}
	}
	
	@ApiOperation("管理员获取实训测试详情")
	@RequestMapping(value = "/trainingTestTest/{id}",method = RequestMethod.GET)
	public Result GetTrainingTestTest(@PathVariable("id")Integer id) {
		TrainingTest trainingTest = trainingTestService.getById(id);
		if (trainingTest == null) {
			return Result.failure(ResultCode.TRAINING_TEST_NOT_EXIST);
		} else {

			return Result.success(trainingTest);
		}
	}
	
	@ApiOperation("管理员修改实训测试状态")
	@RequestMapping(value = "admin/trainingTestTest/{id}/{state}",method = RequestMethod.PUT)
	public Result UpdateTrainingTestTestState(@PathVariable("id")Integer id,@PathVariable("state")Integer state) {
		TrainingTest trainingTest = trainingTestService.getById(id);
		if (trainingTest == null) {
			return Result.failure(ResultCode.TRAINING_TEST_NOT_EXIST);
		} else {
			try {
				trainingTest = trainingTestService.SetTrainingTestState(id, state);
				return Result.success(trainingTest);
			} catch (Exception e) {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		}
	}
	
	@ApiOperation("管理员搜索实训测试列表")
	@RequestMapping(value = "admin/trainingTestTests/{page}/{count}",method = RequestMethod.GET)
	public Result AdminListTrainingTestTest(@PathVariable("page")Integer page,@PathVariable("count")Integer count) {
		return Result.success(trainingTestService.ListTrainingTest(page, count, false));
	}
	
	@ApiOperation("用户搜索实训测试列表")
	@RequestMapping(value = "user/trainingTestTests/{page}/{count}",method = RequestMethod.GET)
	public Result UserListTrainingTestTest(@PathVariable("page")Integer page,@PathVariable("count")Integer count) {
		return Result.success(trainingTestService.ListTrainingTest(page, count, true));
	}
}

